package com.distributor;



import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Favourite extends Activity
{
	ArrayList<ResellerInfo> agent,sales_agent,distributor;
	int AGENT_LEVEL=2,SALES_AGENT_LEVEL=1,DISTRIBUTOR_LEVEL=3;
	Button btnback,btnlogout;
	TextView txtheadername,txtfavmsg;
	
	ListView lv_fav;
	private DataHelper dh;
	private SeparatedListAdapter seprateListAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.favourite);
		
		btnback=(Button)findViewById(R.id.btnPrevious);
		btnlogout=(Button)findViewById(R.id.btnLogout);
		btnback.setVisibility(Button.INVISIBLE);
		
		lv_fav=(ListView)findViewById(R.id.lv_fav);
		txtfavmsg=(TextView)findViewById(R.id.tvNofavouriteMessage);
		txtheadername=(TextView)findViewById(R.id.tv_headerText);
		txtheadername.setText("Favourite");
		
		addFavToListView();
		

		btnlogout.setOnClickListener(new OnClickListener()
		{
			
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				new AlertDialog.Builder(Favourite.this)
		        .setTitle( "Exit Application" )
		        .setMessage( "Are you sure you want to Logout?" )
		        .setPositiveButton("YES", new android.content.DialogInterface.OnClickListener() 
		        {
		            public void onClick(DialogInterface arg0, int arg1) 
		            {
		                //do stuff onclick of YES
		            	setResult(2);
		            	finish();
		            }

					public void onClick(View v) 
					{
						// TODO Auto-generated method stub
						
					}
		        })
		        .setNegativeButton("NO",  new android.content.DialogInterface.OnClickListener() {
		            public void onClick(DialogInterface arg0, int arg1)
		            {
		            	arg0.dismiss();
		            }
		            
					public void onClick(View v) 
					{
						// TODO Auto-generated method stub
					}
					
		        }).show();
				
			}
		});
		
		lv_fav.setOnItemClickListener(new OnItemClickListener() 
		{

			public void onItemClick(AdapterView<?> adapterview, View view, int position,
					long id) 
			{
				AppData.agentInfo=(ResellerInfo)adapterview.getAdapter().getItem(position);
				System.out.println("Getting Name...."+ AppData.agentInfo.getAccountName());
				startActivity(new Intent(Favourite.this, Tab_Reseller.class).putExtra("calleractivity", 10));
				//startActivityForResult(new Intent(Favourite.this, Tab_Reseller.class).putextr, 10);
				
				
			}
		});
	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		new AlertDialog.Builder(Favourite.this)
        .setTitle( "Exit Application." )
        .setMessage( "Are you sure you want to Exit ?" )
        .setPositiveButton("YES", new android.content.DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface arg0, int arg1) 
            {
                //do stuff onclick of YES
            	finish();
            }

			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				
			}
        })
        .setNegativeButton("NO",  new android.content.DialogInterface.OnClickListener() 
         {
         	public void onClick(DialogInterface arg0, int arg1)
            {
                //do stuff onclick of CANCEL
            	arg0.dismiss();
            }

			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
			}
			
        }).show();
	}

	private void addFavToListView()
	{
		seprateListAdapter=new SeparatedListAdapter(this);
		
		agent=getFavourites(AGENT_LEVEL);
		sales_agent=getFavourites(SALES_AGENT_LEVEL);
		distributor=getFavourites(DISTRIBUTOR_LEVEL);
		
		if(agent.isEmpty()&&sales_agent.isEmpty()&&distributor.isEmpty())
		{
			txtfavmsg.setVisibility(TextView.VISIBLE);
			lv_fav.setVisibility(ListView.INVISIBLE);
		}
		else
		{
			if(!agent.isEmpty())
			{
				System.out.println("agent");
				MyListAdapter adapter = new MyListAdapter(this,agent,"topup");
				seprateListAdapter.addSection("Agent",adapter);
			}
			if(!sales_agent.isEmpty())
			{
				System.out.println("sales agent");
				MyListAdapter adapter = new MyListAdapter(this,sales_agent,"electricity");
				seprateListAdapter.addSection("Sales Agent",adapter);
			}
			if(!distributor.isEmpty())
			{
				System.out.println("distributor");
				MyListAdapter adapter = new MyListAdapter(this,distributor,"digitaltv");
				seprateListAdapter.addSection("Distributor",adapter);
			}
			
			lv_fav.setAdapter(seprateListAdapter);
		}
	}

	private ArrayList<ResellerInfo> getFavourites(int i) 
	{

		try
		{
			dh=new DataHelper(Favourite.this);
			return dh.getFavTopUpProducts(i);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
		finally
		{
			if(dh!=null)
				dh.close();
		}
		
	}
	
	 public static class ViewHolder
	 {
			public TextView tvOperatorName;
			public ImageView ivOperatorIcon; 
		 
     }
	
	 class MyListAdapter extends ArrayAdapter<ResellerInfo> 
	 {	
			
		private final Context context;
		ArrayList<ResellerInfo> products;
		 
		
			
		String sectionName;
		LayoutInflater vi ;			  
		
		public MyListAdapter(Context context, ArrayList<ResellerInfo> products, String sectionName)
		{
			super(context, R.layout.row_fav_list, products);
			this.context = context;
			this.products=products;
			 
			this.sectionName=sectionName;
			vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				
		}
 
		 
			public View getView(final int position, View convertView, ViewGroup parent) 
			{
				View v = convertView;
				ViewHolder holder = null;
				
				if (v == null) 
				{
					holder=new ViewHolder();  
					v = vi.inflate(R.layout.row_fav_list, null); 
					holder.tvOperatorName=(TextView) v.findViewById(R.id.tvOperatorName);
					holder.ivOperatorIcon=(ImageView)v.findViewById(R.id.ivOperatorIcon);
					v.setTag(holder);		
				}
				else
					holder=(ViewHolder)v.getTag();
				
				
					System.out.println("Product Name:"+this.products.get(position).getAccountName());
					 
					holder.tvOperatorName.setText(this.products.get(position).getAccountName());
					holder.tvOperatorName.setTag(sectionName);
					 
					
					return v;
			}
			
		}
	}
